package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageBean;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_cate")
public class YddCateController {


    @Autowired
    IYddCateService iyddCateService;

    /**
     * 根据id查询分类信息
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        YddCate yddCate = iyddCateService.getById(id);
        if (yddCate == null) {
            return ResultData.error("没有该分类信息");
        }
        return ResultData.ok("", yddCate);
    }

    /**
     * 分类数据分页查询
     * @param page
     * @param limit
     * @param catename
     * @return
     */
    @GetMapping("/list")
    public ResultData list(int page, int limit, String catename) {
        //1.构建分页查询器
        Page<YddCate> pageInfo = new Page<>(page, limit);
        //2.构建条件查询器
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        //3.添加查询条件
        queryWrapper.like(catename != null && catename != "", YddCate::getCatename, catename);
        queryWrapper.orderByAsc(YddCate::getSortNum);
        //4.查询结果,
        iyddCateService.page(pageInfo, queryWrapper);
        //5.将查询出来的结果数据封装到自定义的分页对象中去
        PageBean<YddCate> pageBean = new PageBean<>();
        pageBean.setCurrentPage(pageInfo.getCurrent());//当前页
        pageBean.setPageSize(pageInfo.getSize());//每页显示条数
        pageBean.setCount(pageInfo.getTotal());//总记录数
        pageBean.setTotalPages(pageInfo.getPages());//总页数
        pageBean.setData(pageInfo.getRecords());//数据集合
        //6.返回响应数据
        return ResultData.ok("",pageBean);
    }

    /**
     * 分类删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData deleteById(Integer id){
        iyddCateService.removeById(id);
        return ResultData.ok("分类删除成功",null);
    }

    /**
     * 修改分类信息
     * @param body
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddCate body){
        iyddCateService.updateById(body);
        return ResultData.ok("分类信息修改成功",null);
    }

    /**
     * 添加分类信息
     * @param body
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddCate body){
        //1.取出分类名称,通过名称判断添加的分类是否已经存在;
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(body.getCatename() != null && body.getCatename() != "",YddCate::getCatename,body.getCatename());
        YddCate one = iyddCateService.getOne(queryWrapper);
        if(one == null){
            //2.如果查询结果为null,则表示该分类没有,可以直接添加
            iyddCateService.save(body);
            return ResultData.ok("添加分类成功",null);
        }
        //3.否则该分类已经存在,提示添加失败;
        return ResultData.error("该分类已存在,请检查后再添加");
    }

    /**
     * 查询分类信息全部
     * @return
     */
    @GetMapping("/all")
    public ResultData all(){
        List<YddCate> list = iyddCateService.list();
        return ResultData.ok("",list);
    }

}
